from urllib.request import urlopen, Request
from lxml import etree
import random
import json
import re

# 第一层页面的链接
url = "https://jumpshop-online.com/collections/%E5%86%8D%E5%85%A5%E8%8D%B7%E3%82%A2%E3%82%A4%E3%83%86%E3%83%A0?filter.v.price.gte=&filter.v.price.lte=&sort_by=manual&c=" + str(random.randint(0, 999999))

req = Request(url)
req.add_header('Cache-Control', 'no-cache')  # 添加 Cache-Control 头

resp = urlopen(req).read().decode("utf-8")
html = etree.HTML(resp)
allHtml = etree.tostring(html, pretty_print=True, encoding='unicode')
regex = r'var meta = ({.*});'
match = re.search(regex, allHtml)
if match:
    meta_string = match.group(1)
    # 将字符串解析成 JSON 对象
    meta_json = json.loads(meta_string)
    # 将 JSON 对象转换成字符串，并以 UTF-8 编码
    meta_utf8 = json.dumps(meta_json, ensure_ascii=False).encode('utf-8')
    print(meta_utf8.decode('utf-8'))
else:
    # 没有找到 var meta = {}，进行错误处理
    pass
